﻿using Loki.UI;
using Loki.Utils;
using NUnit.Framework;

namespace Loki.Core.Test
{
    [TestFixture]
    public class CommandBaseTest : AssertionHelper
    {
        public ICommand Command1 = new LokiRoutedCommand("Command1");
        public ICommand Command2 = new LokiRoutedCommand("Command2");

        [SetUp]
        public void SetUp()
        {
            LokiServices.Command.RegisterCommand(Command1);
            LokiServices.Command.RegisterCommand(Command2);
        }

        [Test]
        public void TestExecute1WithRegisteredHandler()
        {
            bool L_Executed = false;
            ICommandHandler L_Handler = LokiCommandHandler.Create(
                (obj, args) => args.CanExecute |= args.Command.Name == "Command1",
                delegate(object obj, CommandEventArgs args) { L_Executed = true; }
                );

            LokiServices.Command.AddHandler("Command1", L_Handler);

            Command1.Execute(null);

            Expect(
                L_Executed,
                Is.True
                );
        }

        [Test]
        public void TestExecute1WithUnregisteredCommand()
        {
            bool L_Executed = false;
            ICommandHandler L_Handler = LokiCommandHandler.Create(
                (obj, args) => args.CanExecute |= args.Command.Name == "Command1",
                delegate(object obj, CommandEventArgs args) { L_Executed = true; }
                );

            LokiServices.Command.AddHandler("Command1", L_Handler);
            LokiServices.Command.UnregisterCommand(Command1);
            Command1.Execute(null);

            Expect(
                L_Executed,
                Is.False
                );
        }



        [Test]
        public void TestExecute1WithRemovedHandler()
        {
            bool L_Executed = false;
            ICommandHandler L_Handler = LokiCommandHandler.Create(
                (obj, args) => args.CanExecute |= args.Command.Name == "Command1",
                delegate(object obj, CommandEventArgs args) { L_Executed = true; }
                );

            LokiServices.Command.AddHandler("Command1", L_Handler);

            LokiServices.Command.RemoveHandler("Command1", L_Handler);

            Command1.Execute(null);

            Expect(
                L_Executed,
                Is.False
                );
        }

        [Test]
        public void TestExecute2WithRegisteredHandler()
        {
            bool L_Executed = false;
            ICommandHandler L_Handler = LokiCommandHandler.Create(
                (obj, args) => args.CanExecute |= args.Command.Name == "Command1",
                delegate(object obj, CommandEventArgs args) { L_Executed = true; }
                );

            LokiServices.Command.AddHandler("Command2", L_Handler);

            Command2.Execute(null);

            Expect(
                L_Executed,
                Is.False
                );
        }


        [TearDown]
        public void TearDown()
        {
            LokiServices.Command.UnregisterCommand(Command1);
            LokiServices.Command.UnregisterCommand(Command2);
        }
    }
}
